//
//  MHAlertViewController.m
//  MHTextField
//
//  Created by mac on 15/10/23.
//  Copyright (c) 2015年 Mehfuz Hossain. All rights reserved.
//

#import "JGAlertView.h"
#import "DemoTextField.h"

@interface JGAlertView ()

@property (weak, nonatomic) IBOutlet UIView *backgroundView;

@property (weak, nonatomic) IBOutlet UIButton *planType;
@property (weak, nonatomic) IBOutlet UIButton *serveType;

@property (strong, nonatomic) IBOutlet DemoTextField *emailTextField;
@property (strong, nonatomic) IBOutlet DemoTextField *lastNameTextField;
@property (strong, nonatomic) IBOutlet DemoTextField *phoneTextField;
@property (strong, nonatomic) IBOutlet DemoTextField *ageTextField;
@property (strong, nonatomic) IBOutlet DemoTextField *zipTextField;

@end

@implementation JGAlertView

+(instancetype)alertView
{
    return [[[NSBundle mainBundle] loadNibNamed:@"JGAlertView" owner:nil options:nil] lastObject];
}

-(void)awakeFromNib
{
    // 初始化代码
    self.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.1];
    
    //设置scrollView阴影
    self.backgroundView.layer.shadowColor = [UIColor grayColor].CGColor;
    self.backgroundView.layer.shadowOffset = CGSizeMake(0,0);
    self.backgroundView.layer.shadowOpacity = 0.8;
    self.backgroundView.layer.shadowRadius = 4;
    
    [self.planType.layer setBorderWidth: 0.8];
    [self.planType.layer setBorderColor: [UIColor colorWithWhite:0.1 alpha:0.2].CGColor];
    [self.planType.layer setCornerRadius:3.0];
    [self planTypeAction:self.planType];
    
    [self.serveType.layer setBorderWidth: 0.8];
    [self.serveType.layer setBorderColor: [UIColor colorWithWhite:0.1 alpha:0.2].CGColor];
    [self.serveType.layer setCornerRadius:3.0];
    
    //抬头
    [_emailTextField setRequired:YES];
    //收件人
    [_lastNameTextField setRequired:YES];
    //电话
    [_phoneTextField setRequired:YES];
    [_phoneTextField setPhoneField:YES];
    //收货地址
    [_ageTextField setRequired:YES];
    //邮政
    [_zipTextField setRequired:YES];
    [_zipTextField setPostalField:YES];
    
}

- (IBAction)planTypeAction:(UIButton *)sender {
    sender.selected = !sender.isSelected;
    self.serveType.selected = !sender.isSelected;
    if (sender.selected) {
        [sender.layer setBorderColor: [UIColor colorWithRed:47/255.0 green:163/255.0 blue:215/255.0 alpha:1.0f].CGColor];
    } else {
        [sender.layer setBorderColor: [UIColor colorWithWhite:0.1 alpha:0.2].CGColor];
    }
    
    if (self.serveType.selected) {
        [self.serveType.layer setBorderColor: [UIColor colorWithRed:47/255.0 green:163/255.0 blue:215/255.0 alpha:1.0f].CGColor];
    } else {
        [self.serveType.layer setBorderColor: [UIColor colorWithWhite:0.1 alpha:0.2].CGColor];
    }
}

- (IBAction)serveTypeAction:(UIButton *)sender {
    sender.selected = !sender.isSelected;
    self.planType.selected = !sender.isSelected;
    if (sender.selected) {
        [sender.layer setBorderColor: [UIColor colorWithRed:47/255.0 green:163/255.0 blue:215/255.0 alpha:1.0f].CGColor];
    } else {
        [sender.layer setBorderColor: [UIColor colorWithWhite:0.1 alpha:0.2].CGColor];
    }
    
    if (self.planType.selected) {
        [self.planType.layer setBorderColor: [UIColor colorWithRed:47/255.0 green:163/255.0 blue:215/255.0 alpha:1.0f].CGColor];
    } else {
        [self.planType.layer setBorderColor: [UIColor colorWithWhite:0.1 alpha:0.2].CGColor];
    }
}


- (IBAction)createAccount:(id)sender {
    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"提示" message:@"确定提交本发票内容？" delegate:nil cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
    
    if (![self validateInputInView:self.backgroundView]){
        [alertView setMessage:@"填写内容有误或不完整!"];
        
        [alertView show];
    } else {
        //通知代理
        NSDictionary *dict = @{
                               @"vTITLE" : self.emailTextField.text,
                               @"vCONTENT" : (self.planType.selected)? self.planType.currentTitle : self.serveType.currentTitle,
                                @"vRECEIVER" : self.lastNameTextField.text,
                                @"vPHONE" : self.phoneTextField.text,
                                @"vADDRESS" : self.ageTextField.text,
                                @"vZIPCODE" : self.zipTextField.text,
                               };
        
        if ([self.delegate respondsToSelector:@selector(jgAlertView:commitParams:)]) {
            [self.delegate jgAlertView:self commitParams:dict];
            
            [self cancelButton:nil];
        }
    }
}

- (BOOL)validateInputInView:(UIView*)view
{
    for(UIView *subView in view.subviews){
        if ([subView isKindOfClass:[UIScrollView class]])
            return [self validateInputInView:subView];
        
        if ([subView isKindOfClass:[DemoTextField class]]){
            if (![(MHTextField*)subView validate]){
                [(MHTextField*)subView becomeFirstResponder];
                return NO;
            }
        }
    }
    
    return YES;
}


- (IBAction)cancelButton:(id)sender {
    [UIView animateWithDuration:0.2 animations:^{
        self.center = CGPointMake(self.frame.size.width * 0.5, self.frame.size.height + (self.frame.size.height*0.5));
    }];
}


@end
